CREATE TRIGGER [dbo].[Ti_DrugHavaleh] ON [dbo].[DrugHavaleh] 
AFTER INSERT
AS
 Declare @AutoId Bigint
 Declare @AutoIdRow Bigint
 Declare @Last_Id Bigint
 Declare @KCode Varchar(15)
 Declare @Sender Varchar(10)
 Declare @Reciver Varchar(10)
 Declare @Status  Char(1)
 Declare @Price_Kharid  Money
 Declare @Price_Forosh  Money
 Declare @KQty  Float
 Declare @CtrMojodi  Char(1)
 Declare @KindFlag Char(1)
 Declare @Date  DateTime
 Declare @SumForosh  Money
 Declare @MainPrice  Money
 Declare @iCount Int
 Declare @Note NVarchar(50)
 Declare @KindSave Char(1)
 Declare @Recive_Name Nvarchar(35)
 Declare @Trans_Name Nvarchar(35)
 Declare @NoteDet  Varchar(10)
 Declare @K_Code Varchar(15)
 Declare @TotDiffer Money


 Select @KindFlag = DrugH.Kind_Flag, @KindSave =DrugH.Kind_Save From DrugH Inner Join Inserted 
                    On(DrugH.Id_Havaleh = Inserted.Id_Havaleh)
                    Group By DrugH.Kind_Flag,DrugH.Par_no,DrugH.Kind_Save

 Select @Date = Convert(Varchar(10), GetDate(), 111) 
 Select @Last_Id = Id_Havaleh From HeadH Where Tarikh_Faktor = @Date And Kind_Flag = @KindFlag + 6
 IF (@Last_Id Is Null ) Or (@Last_Id = '') Or (@Last_Id = 0)
 Begin 
   IF @KindFlag = '1' 
   Begin
     Set @Note                 = '   '
     Set @Recive_Name =  ' '
     Set @Trans_Name  =  '  '
   End
   Else
   Begin
     Set @Note                 = ' ѐ   '
     Set @Trans_Name   =  ' '
     Set @Recive_Name =  '  '
   End

   Select @NoteDet =  Dbo.MiladiToShamsi(GetDate())
   Set @Note = @Note + @NoteDet

   Insert Into Dbo.HeadH(Kind_Flag,Recive_Name,Trans_Name,Note,User_Id,Tarikh_Faktor)
                  Values(@KindFlag + 6, @Recive_Name, @Trans_Name , @Note , User_Id(SUser_Sname()), Convert(Varchar(10), GetDate(),111))
   Select @Last_Id = @@Identity
 End

 Set @AutoIdRow =Null
 Set @K_Code = Null

 Begin Tran TiDrugHavaleh

 Declare Insert_Cursor Cursor Local For
 Select  AutoId,K_Code,Sender, Reciver, K_Qty1, Price_Kharid, Price_Forosh, CtrMojodi, Status,Main_Price, Tot_Differ From  Inserted
 Open Insert_Cursor

 Fetch Next From Insert_Cursor Into @AutoId, @KCode, @Sender, @Reciver, @KQty, @Price_Kharid, @Price_Forosh, @CtrMojodi, @Status,@MainPrice, @TotDiffer

 While @@Fetch_Status = 0 
 Begin
     IF (@kindSave <> '3' ) 
     begin
        IF  @KindFlag = '1'
        Begin
             Select @K_Code = K_Code, @AutoIdRow = AutoId From Havaleh Where Havaleh.Id_Havaleh = @Last_Id
	         And Havaleh.K_Code =@KCode
             And Sender =@Sender And Price_Forosh = @Price_Forosh           
        End
        Else IF  @KindFlag = '2'
        Begin
             Select @K_Code = K_Code, @AutoIdRow = AutoId  From Havaleh Where Havaleh.Id_Havaleh = @Last_Id
             And Havaleh.K_Code =@KCode
             And Reciver = @Reciver And Price_Kharid = @Price_Forosh
        End

      IF @Status <> '8' 
         IF (@K_Code Is Null  And @AutoIdRow Is Null)
         Begin
     
          Select @iCount = Count(*) From HeadH Inner Join Havaleh 
                        On(HeadH.Id_Havaleh = Havaleh.Id_Havaleh)
                        Where HeadH.Id_Havaleh = @Last_Id And 
                        HeadH.Kind_Flag = @KindFlag + 6

       IF @Status = '7' And @TotDiffer > 0 
          Set @Price_Forosh =  @Price_Forosh  + Round(@TotDiffer/@KQty,0)

       IF @iCount <> 0 
        Set @Status = '1'
       Else
        Set @Status = ''
      
       IF @MainPrice> 0 
           Set @Price_Forosh = @MainPrice
       

       IF @KindFlag = '1'    
           Insert Into Dbo.Havaleh(Id_Havaleh, AutoId,K_Code,Sender, Reciver, K_Qty1, Status, Price_Kharid, Price_Forosh, CtrMojodi ) 
                      Values(@Last_Id, @AutoId, @KCode,@Sender, @Reciver, @KQty, @Status, @Price_Kharid, @Price_Forosh, @CtrMojodi )
         Else
           Insert Into Dbo.Havaleh(Id_Havaleh, AutoId,K_Code,Sender, Reciver, K_Qty1, Status, Price_Kharid, CtrMojodi ) 
                          Values(@Last_Id, @AutoId, @KCode,@Sender, @Reciver, @KQty, @Status, @Price_Forosh, @CtrMojodi )

       IF @@Error > 0                   RollBack Tran TiDrugHavaleh

     End
     Else  
           Update Havaleh Set K_Qty1 = K_Qty1 + @KQty Where AutoId =  @AutoIdRow

     IF @@Error > 0          
      RollBack Tran TiDrugHavaleh

   End 
   Fetch Next From Insert_Cursor Into @AutoId, @KCode, @Sender, @Reciver, @KQty, @Price_Kharid, @Price_Forosh, @CtrMojodi, @Status,@MainPrice, @TotDiffer
   Set @AutoIdRow =Null
   Set @K_Code = Null
 End

 Close Insert_Cursor
 Deallocate Insert_Cursor

 Commit Tran TiDrugHavaleh

 IF @KindFlag = '1'  
 Begin  
    Select @SumForosh = Sum(K_Qty1 * Price_Forosh) From Havaleh Where Id_Havaleh = @Last_Id
    Update HeadH Set Tot_Price_Forosh = @SumForosh Where Id_Havaleh = @Last_Id
 End
 Else 
 Begin 
    Select @SumForosh = Sum(K_Qty1 * Price_Kharid) From Havaleh Where Id_Havaleh = @Last_Id
    Update HeadH Set Tot_Price_Kharid = @SumForosh Where Id_Havaleh = @Last_Id
 End




